package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService;

    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    public R<Page<SetmealDto>> SelectByPage(Integer page, Integer pageSize, String name) {
        //调用自定义分页查询方法
        Page<SetmealDto> pageInfo = setmealService.selectByPage(page, pageSize, name);
        //返回一个分页对象
        return R.success(pageInfo);
    }

    /**
     * 添加套餐
     */
    @PostMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R<String> add(@RequestBody SetmealDto setmealDto) {
        //调用自定义添加方法
        setmealService.add(setmealDto);
        //响应添加成功标识
        return R.success("添加成功");
    }

    /**
     * 根据id查询套餐
     */
    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable Long id) {
         //调用自定义根据id查询方法
        SetmealDto setmealDto = setmealService.selectById(id);
         //响应setmealDto对象
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R<String> update(@RequestBody SetmealDto setmealDto){
          //调用自定义修改方法
        setmealService.updateSetmealDto(setmealDto);

        return R.success("修改成功");
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R<String> delete(Long[] ids){
        setmealService.deleteByIds(ids);

        return R.success("删除成功");
    }

    /**
     * 批量修改状态
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public R<String> updateStatus(@PathVariable Integer status,Long[] ids){
        setmealService.updateStatus(status,ids);

        return R.success("修改成功");
    }


    /**
     * 查询菜品和套餐信息
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "setmeal",key = "#categoryId+'_'+#status",unless = "#result.data.size()<=0")
    public R<List<Setmeal>> list(Long categoryId,Integer status){
        List<Setmeal> list = setmealService.lambdaQuery()
                .eq(categoryId != null, Setmeal::getCategoryId, categoryId)
                .eq(status != null, Setmeal::getStatus, status)
                .list();
        return R.success(list);
    }

    /**
     * 查询套餐具体信息
     */
    @GetMapping("/dish/{setmealId}")
    public R<List<DishDto>> selectSetmealDish(@PathVariable Long setmealId){
       List<DishDto> dto= dishService.selectSetmealDish(setmealId);
        return R.success(dto);
    }



}
